// pages/detail/detail.js
const app = getApp()

import { getpdf, getPrenatalCare } from '../../request/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setMealName: "",
    setMealDetail: [],
    text1: "怀孕初诊 孕周10-12周",
    text2: "高宫，血压，体重，水肿，尿常规，超声波检查，孕中期唐氏综合症筛查",
    selectedIndex: [2, 3, 4, 5, 6],
    text3: "重要提示 Important Information",
    infrotat: "infor",
    text4: "",
    price: "250000",
    text5: "查看基础产检套餐",
    prompt: "",
    selectHeight: "",
    selectedsetMealIndex: undefined,
    selectedweek: undefined,
    checkweekindex: undefined,
    hidebotton: true,
    infomations: undefined,
    openinput: false,
    language: "zh",
    confirm: "确认",
    huise: "灰色套餐不包含",
    inputtext: [],
    showinputtext:'',
    showtext: "",
    information: [],
    selectedmultiple: false,
    body:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    // console.log(options)
    // console.log(app.globalData.language)
    if (options.hide == "true") {
      that.setData({ hidebotton: false })
    }
    getPrenatalCare({ selectedserver: options.selectedserver, selectedsetMealIndex: options.selectedsetMealIndex }).then((res) => {
      let checkweekindex = 0
      if (options.selectedweek < 12) {
        checkweekindex = 0
      } else if (options.selectedweek < 16) {
        checkweekindex = 1
      } else if (options.selectedweek < 20) {
        checkweekindex = 2
      } else if (options.selectedweek < 24) {
        checkweekindex = 3
      } else if (options.selectedweek < 28) {
        checkweekindex = 4
      } else if (options.selectedweek < 30) {
        checkweekindex = 5
      } else if (options.selectedweek < 32) {
        checkweekindex = 6
      } else if (options.selectedweek < 34) {
        checkweekindex = 7
      } else if (options.selectedweek < 36) {
        checkweekindex = 8
      } else if (options.selectedweek < 37) {
        checkweekindex = 9
      } else if (options.selectedweek < 38) {
        checkweekindex = 10
      } else if (options.selectedweek < 39) {
        checkweekindex = 11
      } else if (options.selectedweek < 40) {
        checkweekindex = 12
      }
      res.data.items.forEach((e, index) => {
        // console.log(checkweekindex,index)
        if (checkweekindex > index) {
          e.check = false
        } else {
          e.check = true
        }
        e.detail.forEach(i => {
          if (i.includ.indexOf(Number(options.selectedsetMealIndex)) == -1) {
            i.check = false
          } else {
            i.check = true
          }
        })
      })
      if (app.globalData.language == "zh") {
        that.setData({ language: app.globalData.language })
        // let arr = res.data.infomation.map((e,index) =>{return String(index+1) + "." +e.zh +'\n'+e.en})
        // res.data.infomation.forEach(e=>{
        // let str = arr.join("\n")
        // console.log(str)
        let arr = []
        res.data.infomation.forEach(e => {
          let temp = { zh: e.zh, en: e.en }
          arr.push(temp)
        })
        that.setData({
          setMealDetail: res.data.items,
          setMealName: res.data.name,
          setMealenName: res.data.enName,
          price: res.data.price,
          text4: res.data.name + "优惠价",
          selectedsetMealIndex: Number(options.selectedsetMealIndex),
          selectedweek: Number(options.selectedweek),
          checkweekindex: checkweekindex,
          // textareastr:str,
          information: arr,
          // showtext:str,
          infomations: options
        })
      } else if (app.globalData.language == "en") {
        that.setData({
          language: app.globalData.language,
          huise: "Not Included",
          text3: "Important Information",
          text5: "Check Prenatal Care Package A"
        })
        let arr = res.data.infomation.map((e, index) => { return String(index + 1) + "." + e.en })
        // res.data.infomation.forEach(e=>{
        let str = arr.join("\n")
        // console.log(str)
        that.setData({
          setMealDetail: res.data.items,
          setMealName: res.data.enName,
          price: res.data.price,
          text4: res.data.enName,
          selectedsetMealIndex: Number(options.selectedsetMealIndex),
          selectedweek: Number(options.selectedweek),
          checkweekindex: checkweekindex,
          textareastr: str,
          showtext: str,
          infomations: options
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  more(e) {
    this.data.setMealDetail[e.currentTarget.dataset.index].check = true
    this.setData({
      setMealDetail: this.data.setMealDetail
    })
  },
  hide(e) {
    // this.setData({selectedIndex:-1})
    this.data.setMealDetail[e.currentTarget.dataset.index].check = false
    this.setData({
      setMealDetail: this.data.setMealDetail
    })
  },
  getpdf(e) {
    let data = this.data.infomations
    data.staff = app.globalData.staffId
    data.multiple = this.data.selectedmultiple
    data.body = this.data.body
    data.inputtext = this.data.inputtext
    data.price = this.data.price
    getpdf(data).then((res) => {
      // console.log(res.data)
      wx.downloadFile({
        url: res.data,
        filePath: wx.env.USER_DATA_PATH + '/' + data.name + '.pdf',
        success(res) {
          console.log(res, 'url')
          wx.openDocument({
            filePath: res.filePath,
            fileType: 'pdf',
            showMenu: true
          })
        }
      })
    })
  },
  prompt(e) {
    let temparr = e.detail.value.split('\n')
    let temparr1 = []
    temparr.forEach(e=>{
      // if(e!=""){
        let tempstr = e.split('.').slice(1).join()
        if(tempstr != ""){
          temparr1.push(tempstr)
        }else{
          temparr1.push("")
        }
      // }
    })
    // let tempshowtext = ""
    // let length  = temparr1.length
    // temparr1.forEach((e,index)=>{
    //   let  serial = this.data.information.length+index+1
    //   if(index!=(length-1)){
    //     tempshowtext = tempshowtext + serial + '.' + e + '\n'
    //   }else{
    //     tempshowtext = tempshowtext + serial + '.' + e
    //   }
    // })
    // console.log(temparr1)


    // this.data.prompt = e.detail.value
    // this.data.textareastr = e.detail.value
    // let temp = this.data.textareastr+ '\n'+e.detail.value
    // this.data.infomation = this.data.information.filter((e)=>{return !e.add})
    // console.log(e.detail.value,this.data.information,test)
    // let temparr = e.detail.value.split('\n')
    // temparr.forEach(e=>{
    //   console.log(e.split('.').slice(1).join(),'dasdsadads')
    // })
    // let temp = { zh: e.detail.value, en: "", add: true }
    // let temparr = this.data.information.filter((e) => { return !e.add })
    // if (temp.zh != "" || temp.en != "") {
    //   temparr.push(temp)
    // }
    // this.setData({showtext:temp})
    this.setData({ inputtext: temparr1})
  },
  seebase(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?selectedweek=0&language=' + this.data.language + '&selectedserver=产检&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=1&hide=true'
    })
  },
  inputprompt(e) {
    let tempstr = ""
    let length  = this.data.inputtext.length
    if(this.data.inputtext.length==0){
      let  serial = this.data.information.length+1
      tempstr =  serial + '.'
    }else{
      this.data.inputtext.forEach((e,index)=>{
          let  serial = this.data.information.length+index+1
          if(index!=(length-1)){
            tempstr = tempstr + serial + '.' + e + '\n'
          }else{
            tempstr = tempstr + serial + '.' + e
          }
      })
    }

    this.setData({ openinput: true,showinputtext:tempstr })
  },
  dismiss(e) {
    this.setData({ openinput: false })
  },
  inputstr(e) {
    // console.log(e.detail.value)
    console.log(this.data.textareastr)
    this.setData({ inputvalue: e.detail.value })
  },
  confirm(e) {
    this.setData({ openinput: false })
  },
  multiple(e) {
    const that = this
    if (!this.data.selectedmultiple) {
      let tempprice = that.data.price + (this.data.body) * 7500
      that.setData({ selectedmultiple: true,price:tempprice })
    } else if (this.data.selectedmultiple) {
      let tempprice = that.data.price - (this.data.body * 7500)
      that.setData({ selectedmultiple: false, price: tempprice, body: 1 })
    }
  },
  add(e) {
    let temppeople = this.data.body + 1
    let tempprice = this.data.price + 7500
    this.setData({ body: temppeople, price: tempprice })
  },
  sub(e) {
    const that = this
    if (this.data.body > 1) {
      let temppeople = this.data.body - 1
      let tempprice = this.data.price - 7500
      this.setData({ body: temppeople, price: tempprice })
    }
  },
  linechange(e){
    const that = this
    // console.log(e)
    // console.log(this.data.inputtext)
    if(this.data.inputtext.length!=0){
      let tempshowtext = ""
      let length  = this.data.inputtext.length
      this.data.inputtext.forEach((e,index)=>{
        let  serial = this.data.information.length+index+1
        if(index!=(length-1)){
          tempshowtext = tempshowtext + serial + '.' + e + '\n'
        }else{
          tempshowtext = tempshowtext + serial + '.' + e
        }
      })
      this.setData({showinputtext:tempshowtext})
    }
  }
})